package com.yxp.offer;

import java.util.Arrays;

/**
 * @Auther : yxpweb
 * @Date : 2021/4/27 9:48
 * @Description: 剑指 Offer 58 - II. 左旋转字符串
 */
public class Problem58 {

    public static void main(String[] args) {
        Problem58 problem58 = new Problem58();
        System.out.println(problem58.reverseLeftWords2("lrloseumgh", 6));
    }

    /**
     * @param s lrloseumgh
     * @param n 6
     * @return umghlrlose
     */
    public String reverseLeftWords(String s, int n) {
        char[] sChar = s.toCharArray();
        char[] resultChar = new char[sChar.length];
        int i = n + 1;
        int j = 1;
        resultChar[0] = sChar[n];
        while (i != n) {
            if (i == sChar.length) {
                i = 0;
            }
            resultChar[j] = sChar[i];
            i++;
            j++;
        }
        return new String(resultChar);
    }

    public String reverseLeftWords2(String s, int n) {
        char[] sChar = s.toCharArray();
        char[] charBegin = Arrays.copyOfRange(sChar, 0, n);
        char[] charEnd = Arrays.copyOfRange(sChar, n, sChar.length);
        return new String(charEnd) + new String(charBegin);
    }
}
